package com.apes.fn.server.servicePlan.service;

import com.alibaba.fastjson.JSONArray;
import com.apes.fn.server.servicePlan.repository.ServicePlanDao;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.pi.service.PiManager;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import com.apes.scm.masterdata.product.service.ProductNegativeSellingService;
import com.apes.scm.masterdata.stock.model.Location;
import com.apes.scm.masterdata.stock.service.LocationService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务方案方法
 * Created by lishikun on 2021/7/23 18:28
 */
@Component
public class ServicePlanMethod extends DomainService {

    public static ServicePlanMethod getServicePlanMethodFactory() {
        return new ServicePlanMethod();
    }

    public void setDCParam(Location location, JSONObject param, Boolean switched) {
        com.alibaba.fastjson.JSONObject dcParam = new com.alibaba.fastjson.JSONObject();
        dcParam.put("locationId", location.getId());
        ServicePlanDao servicePlanDao = null;
        JSONArray array = SpringManager.getBean(ServicePlanDao.class).queryProductForDC(dcParam);
        if (!switched) {
            for (Object o : array) {
                JSONObject jsonObject = (JSONObject)o;
                if("000123".equals(jsonObject.getString("DEPOT_LEVEL_ID"))) {
                    param.put("areaLocationId", jsonObject.getString("ID"));
                } else if ("000121".equals(jsonObject.getString("DEPOT_LEVEL_ID"))) {
                    param.put("totalLocationId", jsonObject.getString("ID"));
                }
            }
        } else {
            List areaLocationList = new ArrayList();
            List totalLocationList = new ArrayList();
            for (Object o : array) {
                JSONObject jsonObject = (JSONObject)o;
                if("000123".equals(jsonObject.getString("DEPOT_LEVEL_ID"))) {
                    areaLocationList.add(jsonObject.getString("ID"));
                } else if ("000121".equals(jsonObject.getString("DEPOT_LEVEL_ID"))) {
                    totalLocationList.add(jsonObject.getString("ID"));
                }
            }
            if (areaLocationList.size() == 0) areaLocationList.add("null");
            if (totalLocationList.size() == 0) totalLocationList.add("null");
            param.put("areaLocation", areaLocationList);
            param.put("totalLocation", totalLocationList);
        }
    }

    /**
     * 服务方案
     * @param request
     * @return
     */
    public Object findStockByProduct(JSONObject request, Boolean switched) {
        JSONObject param = new JSONObject();
        try {
            // fixedLossDept 定损
            // belongDept 服务
            // 外采
            JSONObject conditionModel = request.getJSONObject("conditionModel");
            String deptId = conditionModel.get("belongDept") != null ? conditionModel.getJSONObject("belongDept").getString("id") :
                    conditionModel.get("fixedLossDept") != null ? conditionModel.getJSONObject("fixedLossDept").getString("id") : null;
            if (deptId == null) throw new RuntimeException("请传入对应的门店");
            Dept dept = SpringManager.getBean(DeptRepository.class).findOne(deptId);
            param.put("OLD_DEPT_ID", dept.getOrigin()) ;
            param.put("PRODUCT_ID", request.getJSONObject("condition").getString("id")) ;
            Location location = SpringManager.getBean(LocationService.class).findLocationByDept(dept, false);

            ServicePlanFactory.getServicePlanMethod().setDCParam(location, param, switched);
        } catch (Exception e) {
            throw new RuntimeException("无法获取当前门店");
        }
        Map resultMap = null;
        if (!switched) {
            resultMap = findStockOld(param);
        } else {
            resultMap = findStockNew(param);
        }
        return negativeSelling(resultMap,request.getJSONObject("condition").getString("id"));


    }

    /**
     * 商品负卖处理
     * @param map
     * @param productId
     * @return
     */
    private Map negativeSelling(Map map,String productId){
        if (!SpringManager.getBean(ProductNegativeSellingService.class).isProductNegativeSelling(productId)) return map;
        List generalList = (List) map.get("general");
        generalList.add(MapUtil.mapper(
                        "stockType", "可负卖库存",
                        "stock", 99));
        map.put("general",generalList);
        return map;
    }

    private Map findStockOld(JSONObject param) {
        List subList = new ArrayList();
        List generalList = new ArrayList();
        JSONObject jsonObject = SpringManager.getBean(PiManager.class).synErp("mdmCkkqService.findCKKQByDdid", param).getJSONObject("obj");
        JSONArray array = SpringManager.getBean(ServicePlanDao.class).findStockByProduct(param);
        JSONObject stockByProduct = array.size() > 0 ? array.getJSONObject(0) : new JSONObject();
        if (stockByProduct.getString("SUB_STOCK_COMPANY_ID") != null) {
            //SUB
            subList.add(
                    MapUtil.mapper("companyId", stockByProduct.getString("SUB_STOCK_COMPANY_ID"),
                            "companyName", stockByProduct.getString("SUB_STOCK_COMPANY_NAME"),
                            "stock", stockByProduct.getInteger("NEW_SUB_STOCK"))
            );
        }
        // GENERAL
        if (stockByProduct.getString("GENERAL_STOCK_COMPANY_ID") != null) {
            generalList.add(
                    MapUtil.mapper("companyId", stockByProduct.getString("GENERAL_STOCK_COMPANY_ID"),
                            "companyName", stockByProduct.getString("GENERAL_STOCK_COMPANY_NAME"),
                            "stock", stockByProduct.getInteger("NEW_GENERAL_STOCK"))
            );
        }
        if (jsonObject.getJSONArray("QYC").size() != 0) {
            subList.add( MapUtil.mapper("companyId",
                    jsonObject.getJSONArray("QYC").size() != 0  ?
                            jsonObject.getJSONArray("QYC").getJSONObject(0).getString("GSID") : null,
                    "companyName", jsonObject.getJSONArray("QYC").size() != 0 ?
                            jsonObject.getJSONArray("QYC").getJSONObject(0).getString("GSJC") :null,
                    "stock", stockByProduct.getInteger("OLD_SUB_STOCK")));
        }
        if (jsonObject.getJSONArray("ZCK").size() != 0) {
            generalList.add( MapUtil.mapper("companyId", jsonObject.getJSONArray("ZCK").size() != 0 ?
                            jsonObject.getJSONArray("ZCK").getJSONObject(0).getString("GSID") : null,
                    "companyName", jsonObject.getJSONArray("ZCK").size() != 0 ?
                            jsonObject.getJSONArray("ZCK").getJSONObject(0).getString("GSJC") : null,
                    "stock", stockByProduct.getInteger("OLD_GENERAL_STOCK")));
        }
        return MapUtil.mapper("area", subList, "general", generalList);
    }

    private Map findStockNew(JSONObject param) {
        List subList = new ArrayList();
        List generalList = new ArrayList();
        JSONArray areaLocation = param.getJSONArray("areaLocation");
        for (Object o : areaLocation) {
            param.put("areaLocationId", o);
            JSONArray array = SpringManager.getBean(ServicePlanDao.class).findStockByProduct(param);
            JSONObject stockByProduct = array.size() > 0 ? array.getJSONObject(0) : new JSONObject();

            if (stockByProduct.getString("SUB_STOCK_COMPANY_ID") != null) {
                //SUB
                subList.add(
                        MapUtil.mapper("companyId", stockByProduct.getString("SUB_STOCK_COMPANY_ID"),
                                "companyName", stockByProduct.getString("SUB_STOCK_COMPANY_NAME"),
                                "stock", stockByProduct.getInteger("NEW_SUB_STOCK"))
                );
            }
        }
        JSONArray totalLocation = param.getJSONArray("totalLocation");
        for (Object o : totalLocation) {
            param.put("totalLocationId", o);
            JSONArray array = SpringManager.getBean(ServicePlanDao.class).findStockByProduct(param);
            JSONObject stockByProduct = array.size() > 0 ? array.getJSONObject(0) : new JSONObject();
            // GENERAL
            if (stockByProduct.getString("GENERAL_STOCK_COMPANY_ID") != null) {
                generalList.add(
                        MapUtil.mapper("companyId", stockByProduct.getString("GENERAL_STOCK_COMPANY_ID"),
                                "companyName", stockByProduct.getString("GENERAL_STOCK_COMPANY_NAME"),
                                "stock", stockByProduct.getInteger("NEW_GENERAL_STOCK"))
                );
            }
        }
        return MapUtil.mapper("area", subList, "general", generalList);

    }

}
